package com.commons.interfaces.MyRibbonRuleConfig;


import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * @PROJECT_NAME: SpringCloud_AtGuiGu
 * @NAME: MyRibbonConfig
 * @Author: lidongming
 * @DATE: 2022/2/10
 * @Desc:
 */
//这里不能添加@Configuration 不然全部子模块都会用这个策略 不能达到指定客户端配置
public class MyRibbonConfig {


    /**
     * 启动 随机的负载均衡策略
//     * @return
//     */
//    @Bean
//    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
//                                                                                   LoadBalancerClientFactory loadBalancerClientFactory) {
//        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
//        return new RandomLoadBalancer(
//                loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
//    }
//
    @Bean
    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,

                                                                                   LoadBalancerClientFactory loadBalancerClientFactory) {
        int weight [] = {3,7};
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new WriteMyRibbon(
                loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name, weight);
    }


}
